
var typeDatas = [
    { id: "int", text: "整数类型" },
    { id: "varchar(255)", text: "字符串类型" },
    { id: "datetime", text: "时间类型" },
    { id: "numeric", text: "小数类型" }
];
var isno = [
    {id: "true", text: "是"},
    {id: "false", text: "否"}
];
mini.parse();
var grid = mini.get("datagrid1");
var grid2 = mini.get("datagrid2");
var editForm = document.getElementById("editForm1");
var addForm = document.getElementById("addForm2");
var tableClassId;
var tableClassName;
grid.load(ajaxUrl + "/tableClass/findAll.do");
//grid.load(ajaxUrl + "/tableClass/list.do");
grid.load();
var url = bootPATH + "../frame/frame1/pages/";      //路径前缀
//表格联动
function SelectionChanged(e){
    //var grid = e.sender;
    var record = grid.getSelected();
    tableClassId = record.tableClassId;
    tableClassName = record.tableClassName;
    if (record) {
        //请求表详细信息
        $.ajax({
            //url: ajaxUrl + "/classTable/findByTableId.do?tableClassId="+tableClassId,
            url: ajaxUrl + "/classTable/findByTableId.do?tableClassId=" + tableClassId,
            type: "post",
            //dataType: "json",
            traditional: true,
            success: function(data) {
                console.log(data);
                grid2.setData(data.data);
            },
            error : function(data){
                mini.alert('失败');
            }
        });
    }
}
//查询
function search() {
    var key = mini.get("key").getValue();
    grid.load({ key: key });
}
function onKeyEnter(e) {
    search();
}
//主子表关系
function relationship(){
    var row = grid.getSelected();
    if(row){
        //弹出主子表关系页面
        var tableClassId = row.tableClassId;
        var tableClassName = row.tableClassName;
        mini.open({
            url: url +"alertBox/masterTable.html",
            title: "主子表关系", width: 1000, height: 600,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = {
                    action: "relationship",
                    tableClassId : tableClassId,
                    tableClassName : tableClassName
                };
                iframe.contentWindow.Delivery(data);
            },
            ondestroy: function (action) {
                grid.reload();
            }
        });
    }else{
        mini.alert("请选择一条数据");
    }

}
//行内编辑按钮
function onActionRenderer(e) {
    var grid = e.sender;
    var record = e.record;
    var uid = record._uid;
    var rowIndex = e.rowIndex;

    var s = '<a class="New_Button" href="javascript:newRow()">新增</a> '
        + '<a class="Edit_Button" href="javascript:editRow(\'' + uid + '\')">编辑字段</a> ';

    return s;
}
//新增行
function newRow() {
    var row = {};
    grid2.addRow(row, 0);
    grid2.hideAllRowDetail();       //隐藏行详细
    grid2.showRowDetail(row);       //显示行详细(显示该行详细信息单元格)
    //将editForm元素(详细信息表单元素)，加入行详细单元格内
    var td = grid2.getRowDetailCellEl(row);
    td.appendChild(addForm);
    addForm.style.display = "";    //显示编辑详细表单
    //editRow(row._uid);
}
//编辑行
function editRow(row_uid) {
    //得到当前被修改的行
    var row = grid2.getRowByUID(row_uid);
    //如果选中了
    if (row) {
        grid2.hideAllRowDetail();       //隐藏行详细
        grid2.showRowDetail(row);       //显示行详细(显示该行详细信息单元格)

        //将editForm元素(详细信息表单元素)，加入行详细单元格内
        var td = grid2.getRowDetailCellEl(row);
        td.appendChild(editForm);
        editForm.style.display = "";    //显示编辑详细表单

        //表单加载数据库详细信息(原有信息)
        var form = new mini.Form("editForm1");
        if (grid2.isNewRow(row)) {
            form.reset();
        } else {
            form.loading();     //等待加载
            var obj = {
                tableId : row.tableId,
                fieldCh : row.fieldCh
            }
            form.setData(obj);      //赋值(原始值)
            form.unmask();          //加载完毕取消遮罩
        }
        grid2.doLayout();
    }
}
//更新修改后的数据
function updateRow() {
    //得到需要修改的表单
    var form = new mini.Form("editForm1");
    //得到值
    var o = form.getData();
    //编辑
    grid2.loading("保存中，请稍后......");
    var editTable = {
        id : o.tableId,
        fieldCh : o.fieldCh
    }
    var jsonString = JSON.stringify(editTable);
    console.log(jsonString);
    //保存编辑
    $.ajax({
        url: ajaxUrl + "/classTable/updateTable.do",
        type : "POST",
        contentType : "application/json",
        data: jsonString,
        success: function (data) {
            //成功提示
            mini.alert(data.data);
            grid2.unmask();
            //请求显示
            $.ajax({
                //url: ajaxUrl + "/classTable/findByTableId.do?tableClassId="+tableClassId,
                url: ajaxUrl + "/classTable/findByTableId.do?tableClassId=" + tableClassId,
                type: "post",
                //dataType: "json",
                traditional: true,
                success: function(data) {
                    console.log(data);
                    grid2.setData(data.data);
                },
                error : function(data){
                    mini.alert('失败');
                }
            });
            //grid2.reload();
        },
        error: function (data) {
            console.log(data);
        }
    });
}
//新增保存
function saveRow(){
    var addForm = new mini.Form("addForm2");
    //得到值
    var o = addForm.getData();
    //form.unmask();
    var addTable = {
        tableName : tableClassName,
        fieId : o.fieId,
        fieldCh : o.fieldCh,
        type : o.type,
        notNull : o.notNull
    }
    var jsonString = JSON.stringify(addTable);
    grid2.loading("保存中，请稍后......");
    //保存编辑
    $.ajax({
        url: ajaxUrl + "/Base/insertField.do",
        type : "POST",
        contentType : "application/json",
        data: jsonString,
        success: function (data) {
            grid2.unmask();
            //成功提示
            mini.alert(data.data);
            //请求显示
            $.ajax({
                url: ajaxUrl + "/classTable/findByTableId.do?tableClassId=" + tableClassId,
                type: "post",
                //dataType: "json",
                traditional: true,
                success: function(data) {
                    grid2.setData(data.data);
                },
                error : function(data){
                    mini.alert('失败');
                }
            });
            //grid2.reload();
        },
        error: function (data) {
            console.log('请求失败');
        }
    });
}
//行内编辑新增关闭
function cancelRow(){
	SelectionChanged();
}





